﻿Public Class Varesalg
    Public Property id As ULong
    Public Property leveringsAdresse As String
    Public Property status As String
    Public Property leveringsDato As DateTime
    Public Property bestillingsDato As DateTime
    Public Property salgsBeløp As Single
    Public Property kundeNr As Integer
    Public Property selgerNr As Integer

    Public Sub New()
    End Sub

    Public Function FinnInnkjopspris() As Integer
        Dim innPris As Integer = 0
        Dim alleKakepakker As List(Of KakePakke) = KakePakke.FinnAlle()
        For Each kp In alleKakepakker
            If kp.vasresalgId = id Then 'tilhører dette varesalger
                innPris += kp.beregnInnkjopsPris()
            End If
        Next

        Return innPris

    End Function

    Public Shared Function FinnLevDatoOgAntall() As Dictionary(Of String, Integer)
        Dim DatoOgAnt As New Dictionary(Of String, Integer)
        Dim søkestreng As String = "SELECT leveringsdato AS Dato, Count(*) AS ant FROM varesalg Group By leveringsdato"

        Dim dt As New DataTable
        dt = Database.Query(søkestreng)

        Dim row As DataRow
        For Each row In dt.Rows

            DatoOgAnt.Add(row("Dato"), row("ant"))

        Next row

        Return DatoOgAnt

    End Function

    Public Shared Function FinnAlle() As List(Of Varesalg)
        Dim søkestreng As String = "SELECT * FROM varesalg"
        Dim vsTabell As New DataTable
        vsTabell = Database.Query(søkestreng)
        Dim vsListe As New List(Of Varesalg)

        Dim row As DataRow
        For Each row In vsTabell.Rows
            Dim etVaresalg As New Varesalg
            etVaresalg.id = row("varesalgsId")
            etVaresalg.leveringsAdresse = row("leveringsAdresse")
            etVaresalg.bestillingsDato = row("bestillingsDato")
            etVaresalg.leveringsDato = row("leveringsDato")
            etVaresalg.status = row("status")
            etVaresalg.salgsBeløp = row("varesalgsBelop")
            etVaresalg.kundeNr = row("kundeNr")
            etVaresalg.selgerNr = row("ansattNr")

            vsListe.Add(etVaresalg)

        Next row

        Return vsListe

    End Function


    Public Shared Function FinnAlleSalgsbelop() As List(Of Varesalg)
        Dim søkestreng As String = "SELECT * FROM varesalg"
        Dim vsTabell As New DataTable
        vsTabell = Database.Query(søkestreng)
        Dim vsListe As New List(Of Varesalg)

        Dim row As DataRow
        For Each row In vsTabell.Rows
            Dim etVaresalg As New Varesalg
            etVaresalg.id = row("varesalgsId")
            etVaresalg.salgsBeløp = row("varesalgsBelop")

            vsListe.Add(etVaresalg)

        Next row

        Return vsListe

    End Function




    Public Function Lagre(ByVal adresse, ByVal bestDato, ByVal levDato, ByVal status, ByVal sum, ByVal kNr, ByVal ansNr, ByVal abbId) As Integer

        Dim PK As Integer = 0

        Dim søkestreng As String = "INSERT INTO varesalg (leveringsAdresse, bestillingsDato, leveringsDato, status, varesalgsBelop, kundeNr, ansattNr, abonnementId)"
        søkestreng &= " VALUES ('" & adresse & "','" & bestDato & "','" & levDato & "','" & status & "','" & sum & "','" & kNr & "','" & ansNr & "','" & abbId & "')"

        Dim dt As New DataTable
        dt = Database.Query(søkestreng)

        søkestreng = "Select MAX(varesalgsId) AS MAX FROM varesalg" 'henter siste PK som skal være FK i tilhørende kakepakker
        dt = Database.Query(søkestreng)

        Dim row As DataRow
        For Each row In dt.Rows
            PK = row(0)
        Next

        Return PK ' Returnerer primærnøkkel som skal være FK i kakepakker som tilhører varesalget

    End Function

End Class